Type.registerNamespace('ScottCate');

ScottCate.Automobile = function(color) {
    this._color = color;
    this._maxSpeed = 0;
    this._numberOfWheels = 4;
    this._currentSpeed = 0;
    this._events = new Sys.EventHandlerList();
}

ScottCate.Automobile.prototype = {
    get_color: function() { return this._color; },
    get_maxSpeed: function() { return this._maxSpeed; },
    get_wheelCount: function() { return this._numberOfWheels; },
    get_currentSpeed: function() { return this._currentSpeed; },
    get_events: function() { return this._events; },

    increaseSpeed: function(speed) {
        this._currentSpeed += speed;
        if (this._currentSpeed > this._maxSpeed) {
            var overage = this._currentSpeed - this._maxSpeed;
            this._currentSpeed = this._maxSpeed;
            this.raiseEvent('overHeat', {OverMaxSpeed: overage});
        }
        return this._currentSpeed;
    },

    decreaseSpeed: function(speed) {
        if (this._currentSpeed <= speed)
            this._currentSpeed = 0;
        else
            this._currentSpeed -= speed;

        return this._currentSpeed;
    },

    add_overHeat: function(handler) {
        this.get_events().addHandler('overHeat', handler);
    },
    remove_overHeat: function(handler) {
        this.get_events().removeHandler('overHeat', handler);
    },
    raiseEvent: function(eventName, eventArgs) {
        // Get handler for event.   
        var handler = this.get_events().getHandler(eventName);

        if (handler) {
            if (!eventArgs) { eventArgs = Sys.EventArgs.Empty; }

            // Fire event.                             
            handler(this, eventArgs);
        }
    }
}

ScottCate.Automobile.registerClass('ScottCate.Automobile', null, Sys.IDisposable);

//Define and register Cheap Car
ScottCate.CheapCar = function(color) {
    ScottCate.CheapCar.initializeBase(this, [color]);
    this._maxSpeed = 55;
}
ScottCate.CheapCar.registerClass('ScottCate.CheapCar', ScottCate.Automobile);

//Define and register Corvette
ScottCate.Corvette = function(color) {
    ScottCate.Corvette.initializeBase(this, [color]);
    this._maxSpeed = 155;
}
ScottCate.Corvette.registerClass('ScottCate.Corvette', ScottCate.Automobile);

//Define and register Ferrari
ScottCate.Ferrari = function(color) {
    ScottCate.Ferrari.initializeBase(this, [color]);
    this._maxSpeed = 255;
}
ScottCate.Ferrari.registerClass('ScottCate.Ferrari', ScottCate.Automobile);

//Define and register Dump Truck
ScottCate.Dumptruck = function(color) {
    ScottCate.Dumptruck.initializeBase(this, [color]);
    this._maxSpeed = 15;
}
ScottCate.Dumptruck.registerClass('ScottCate.Dumptruck', ScottCate.Automobile);

//Define and register Motocycle
ScottCate.Motorcycle = function(color) {
    ScottCate.Motorcycle.initializeBase(this, [color]);
    this._maxSpeed = 65;
    this._numberOfWheels = 2;
}
ScottCate.Motorcycle.registerClass('ScottCate.Motorcycle', ScottCate.Automobile);

//Define and register Speed Bike
ScottCate.SpeedBike = function(color) {
    ScottCate.SpeedBike.initializeBase(this, [color]);
    this._maxSpeed = 233;
}
ScottCate.SpeedBike.registerClass('ScottCate.SpeedBike', ScottCate.Motorcycle);

ScottCate.SportsCar = function(speed) {
    ScottCate.SportsCar.initializeBase(this, [speed]);
    this.set_maxSpeed(speed);
}

ScottCate.SportsCar.prototype = {
    set_maxSpeed: function(speed) {
        if(speed < 150) {
            var message = "Sports Cars have to be able to reach 150 MPH";
            var err = Error.argumentOutOfRange('speed', speed, message);
            throw err;
        }
        else {
            this._maxSpeed = speed;
        }
    }
}
ScottCate.SportsCar.registerClass('ScottCate.SportsCar', ScottCate.Automobile);













